
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public final class DBManager {

    private DBManager() {
    }

    public static Connection getConnection() throws Exception {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager
                .getConnection(
                        "jdbc:mysql://localhost:13306/rtp?useUnicode=true&characterEncoding=UTF-8",
                        "root", "15068661");
        return con;
    }

    public static void closeConnection(Connection con, Statement stmt,
            ResultSet rs) throws Exception {
        closeResultSet(rs);
        closeStatement(stmt);
        closeConnection(con);
    }

    public static void closeConnection(Connection con, Statement stmt)
            throws Exception {
        closeStatement(stmt);
        closeConnection(con);
    }

    private static void closeResultSet(ResultSet rs) throws Exception {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                throw e;
            }
        }
    }

    private static void closeStatement(Statement stmt) throws Exception {
        if (stmt != null) {
            try {
                stmt.close();
            } catch (Exception e) {
                throw e;
            }
        }
    }

    private static void closeConnection(Connection con) throws Exception {
        if (con != null) {
            try {
                con.close();
            } catch (Exception e) {
                throw e;
            }
        }
    }
}
